<?php

/**
 * user actions.
 *
 * @package    dnevnik
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class userActions extends sfActions
{
  private function authenticate($profil)
  {
      $profil->setLastActiv(time());
      $profil->save();
      $this->getUser()->setAuthenticated(true);
      $this->getUser()->setAttribute("id", $profil->getId());
      $this->getUser()->setAttribute("tip", $profil->getTip());

      $credentials = new Credentials();
      $credentials->addCredentials();
  }

  /**
  *@desc klasicna autentifikacija, provera username-a i passworda
  */
  public function executeLogin($id)
  {
      $username = $this->getRequestParameter('username');
      $password = $this->getRequestParameter('password');

      $profil = DbFinder::from('Profil')->where('Tip', ProfilPeer::$tip['upravnik'])->where('Username', $username)->where('Password', md5($password))->findOne();
      if($profil)
      {
        $this->authenticate($profil);
      }

      $this->redirect('@homepage');
      return sfView::NONE;
  }

  /**
  *@desc logout i redirect na homepage
  */
  public function executeLogout(sfWebRequest $request)
  {
      if($this->getUser()->getAttribute('id'))
      {
         $logiranje = new Logiranje();
         $logiranje->setProfilId($this->getUser()->getAttribute('id'));
         $logiranje->setTip(LogiranjePeer::$tip['logout']);
         $logiranje->save();
      }

      $this->getUser()->setAuthenticated(false);
      $this->getUser()->clearCredentials();
      $this->getUser()->getAttributeHolder()->clear();
      header("Location: /public.php");
      die();
      $this->redirect('@homepage');
      return sfView::NONE;
  }

  public function executeSettings(sfWebRequest $request)
  {
      $profil = DbFinder::from('Profil')->join('Upravnik')->with('Upravnik')->findPk($this->getUser()->getAttribute('id'));
      if($request->isMethod('post'))
      {
          $this->error = $this->settingsValidacija($request, $profil);
          if(!$this->error)
          {
              $profil->setIme($request->getParameter('ime'));
              $profil->setPrezime($request->getParameter('prezime'));
              if($profil->getUsername() != $request->getParameter('username'))
                $profil->setUsername($request->getParameter('username'));
              if($request->getParameter('password') != '')
                $profil->setPlainPassword($request->getParameter('password'));

              if($request->getParameter('pol'))
                $profil->setPol(ProfilPeer::$pol[$request->getParameter('pol')]);

              if($profil->getEMail() != $request->getParameter('e_mail'))
                $profil->setEMail($request->getParameter('e_mail'));

              if($profil->save())
                $this->error = '<li class="saved">'.$this->getContext()->getI18N()->__('Podešavanja su sačuvana!').'</li>';
          }
      }

      $this->profil = $profil;
  }

  private function settingsValidacija($request, $profil)
  {
      $err = '';
      if($profil->getUsername() != $request->getParameter('username'))
      {
        if(strlen($request->getParameter('username')) < 2)
          $err .= '<li>'.$this->getContext()->getI18N()->__('Korisničko ime je prekratko!').'</li>';
        if(DbFinder::from('Profil')->where('Username', $request->getParameter('username'))->count())
          $err .= '<li>'.$this->getContext()->getI18N()->__('Korisničko ime već postoji u bazi!').'</li>';
      }
      if($request->getParameter('password') != '')
      {
        if(strlen($request->getParameter('password')) < 2)
            $err .= '<li>'.$this->getContext()->getI18N()->__('Šifra je prekratka!').'</li>';
        if($request->getParameter('password') != $request->getParameter('repeat'))
            $err .= '<li>'.$this->getContext()->getI18N()->__('Šifre se ne slažu!').'</li>';
      }

    if(strlen($err) > 0)
      return $err;
    return FALSE;
  }
}
